package com.stormrage.edu.ctrl.learn;

import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.stormrage.edu.serv.edu.EduMajorServ;
import com.stormrage.edu.serv.learn.LeaStudentVideoServ;
import com.stormrage.edu.serv.student.StuAcctServ;
import com.stormrage.entity.learn.rep.LeaMajorStudyRep;
import com.stormrage.entity.learn.view.LeaStudentVideo;
import com.stormrage.entity.student.StuAcct;
import com.stormragetech.core.ctrl.BaseCtrl;

/**
 * @Title: LeaStudyCtrl.java
 * @Package com.stormrage.edu.ctrl.learn
 * @Description: 学习 Controller
 * @author www.stormragetech.com
 * @date 2016年5月11日
 * @version V1.0
 */
@Controller
@RequestMapping("/study")
public class LeaStudyCtrl extends BaseCtrl {

	private static final Logger logger = LoggerFactory.getLogger(LeaStudyCtrl.class);

	@Value("#{constants['html.title']}")
	private String htmlTitle;

	@Value("#{constants['html.logo']}")
	private String htmlLogo;

	@Value("#{constants['url.root']}")
	private String rootUrl;

	@Value("#{constants['path.image']}")
	private String imagePath;

	@Autowired
	private EduMajorServ eduMajorServ;

	@Autowired
	private LeaStudentVideoServ leaStudentVideoServ;

	@Autowired
	private StuAcctServ stuAcctServ;

	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String index(HttpServletRequest request, Model model) throws UnsupportedEncodingException {

		logger.debug("【 Controller 】-【 LeaStudyCtrl 】-【 Index 】-【 GET 】-【 STARTED 】");

		try {

			// ------------------ 获取用户信息 ------------------
			Subject currentUser = SecurityUtils.getSubject();
			StuAcct curr = stuAcctServ.queryByAcct(currentUser.getPrincipal().toString());

			// ------------------ Controller 操作 ------------------
			List<LeaMajorStudyRep> array = eduMajorServ.queryListForStudy(curr.getId());

			// ------------------ Model 赋值 ------------------
			model.addAttribute("ipath", rootUrl + imagePath);
			model.addAttribute("user", curr);
			model.addAttribute("data", array);

		} catch (Exception e) {
			model.addAttribute("err", e.getMessage());
			return "error/500";
		}

		logger.debug("【 Controller 】-【 LeaStudyCtrl 】-【 Index 】-【 GET 】-【 COMPLETED 】");
		return "learn/index";
	}

	@RequestMapping(value = "/time/{id}", method = RequestMethod.POST)
	@ResponseBody
	public String time(@PathVariable("id") Integer id, @RequestParam(value = "time", required = false) Integer time,
			HttpServletRequest request) throws UnsupportedEncodingException {

		logger.debug("【 Controller 】-【 LeaStudyCtrl 】-【 time 】-【 GET 】-【 STARTED 】");

		try {

			// ------------------ Controller 操作 ------------------
			LeaStudentVideo entity = leaStudentVideoServ.queryById(id);
			entity.setTime(time);
			leaStudentVideoServ.update(entity);

		} catch (Exception e) {
			return "error/500";
		}

		logger.debug("【 Controller 】-【 LeaStudyCtrl 】-【 time 】-【 GET 】-【 COMPLETED 】");
		return time.toString();
	}

}
